$(document).ready(function(){

	$("#display").mouseleave(function(){
		$("#display").hide();
	});
	
	$(".search").keyup(function() 
	{
		var searchbox = $(this).val();
		var dataString = 'searchword='+ searchbox;
	
		if(searchbox != '')
		{	
			$.ajax({
				type: "POST",
				dataType : "json",
				url: "/Application/search",
				data: dataString,
				cache: false,
				success: function(json)
				{
					$("#display").children("li").remove();
					$("#display").append("<li class='nav-header'>Utilisateurs</li>")
					
						$.each(json.elements,function(i,n){
							$("#display").append("<li class='display_box'></li>");
							var displayBox = $("#display").find(".display_box:last");
							displayBox.append("<a href='/Profil/"+n.id+"/"+n.lastname+" "+n.firstname+"'></a>");
							var link = displayBox.find("a:first");
							link.append("<img src='/interventions/image?id="+n.id+"' style='width:40px; float:left; margin-right:6px'>");
							link.append(n.firstname.replace(searchbox,"<b>"+searchbox+"</b>")+" "+n.lastname.replace(searchbox,"<b>"+searchbox+"</b>"));
							link.append("<br>");
							link.append("<span style='font-size:9px; color:#999999'>"+n.type+"</span>");
						});	
					if(json.elements.length == 0){
						$("#display").append("<li class='display_box'><span style='font-size:9px; color:#999999'>Aucun résultat</span></li>");
					}
					$(display).show();
				}
			});
		}
		
		return false;    
	});
});